package com.his.common.util;

import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

/**
 * MD5密码加密工具
 */
@Component
public class MD5Util {
    private static String encryStr = "abcdef";

    /**
     * md5加密（两次）
     * @param password
     * @return
     */
    public static String MD5Str(String password){
        String encrytStr = encrypt(password);
        return DigestUtils.md5DigestAsHex(DigestUtils.md5DigestAsHex(encrytStr.getBytes()).getBytes());
    }

    /**
     * 加密字符串
     * @param password
     * @return
     */
    public static String encrypt(String password){
        if(StringUtils.isEmpty(password)){
            return null;
        }
        char[] newPassword = password.toCharArray();
        if(newPassword.length>=6) {
            StringBuffer returnStr = new StringBuffer();
            char[] encryt = encryStr.toCharArray();
            for (int i = 0; i < newPassword.length; i++) {
                returnStr.append(newPassword[i]);
                if(i < encryt.length){
                    returnStr.append(encryt[i]);
                }
            }
            return returnStr.toString();
        }
        return password;
    }


    public static void main(String[] args) {
       System.out.println(MD5Str("12345678"));
    }
}
